//
//  WebBrowserViewController.m
//  mUBB
//
//  Created by Cosmin Stirbu on 2/28/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "WebBrowserViewController.h"

@interface WebBrowserViewController ()

@end

@implementation WebBrowserViewController
@synthesize webView, toolBar, backButton, forwardButton;

- (id)initWithNibName:(NSString *)nibNameOrNil urlString:(NSString *)url
{
    self = [super initWithNibName:nibNameOrNil bundle:nil];
    if (self) {
        // Custom initialization
        _url = url;
    }
    return self;
}	

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:_url]];
    [webView loadRequest:request];
    [webView setScalesPageToFit:YES];
    toolBar.tintColor = self.navigationController.navigationBar.tintColor;
    
}

#pragma mark - UI Interaction
- (IBAction)backButtonTapped:(id)sender {
    [webView goBack];
}

- (IBAction)forwardButtonTapped:(id)sender {
    [webView goForward];
}

- (IBAction)refreshButtonTapped:(id)sender {
    [webView reload];
}

#pragma mark - UIWebViewDelegate Methods

- (void)webViewDidStartLoad:(UIWebView *)_webView {
    [Utils showLoadingOn:self];
}

- (void)webViewDidFinishLoad:(UIWebView *)_webView {
    [Utils hideLoadingOn:self];
    if ([webView canGoBack]) {
        [backButton setEnabled:YES];
    } else {
        [backButton setEnabled:NO];
    }
    if ([webView canGoForward]) {
        [forwardButton setEnabled:YES];
    } else {
        [forwardButton setEnabled:NO];
    }
    [webView setScalesPageToFit:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewDidUnload {
    [self setWebView:nil];
    [self setToolBar:nil];
    [self setBackButton:nil];
    [self setForwardButton:nil];
    [super viewDidUnload];
}
@end
